<?php

/**
 * @author Martin Pernica
 * @copyright 2009
 */

class User extends System
{
    
    public static $_cache_display_name = array();
    public static $_cache_avatar = array();

    /**
     * User::__construct()
     * 
     * @return
     */
    public function __construct()
    {
    }

    /**
     * Get a ID of current user
     * 
     * @return int
     */
    public function getUid()
    {
        return intval($_SESSION['id']);
    }
     
	/**
     * Do login request
     * 
     * @return bool
     */
    public function doLogin($username, $password)
    {
        $hash = new Hash($username, $password);
        $hash->hash();
        $password = $hash->password;
        unset($hash);

        $query = Registry::get('db')->query("SELECT id, rank FROM {prefix}users WHERE username = '" .
            $GLOBALS['db']->real_escape_string($username) . "' AND password = '" . $password .
            "'");
        if ($query->num_rows != 0) {
            $data = $query->fetch_array();
            $last_login = Registry::get('db')->query("UPDATE {prefix}users SET last_login = NOW() WHERE id = '" .
                intval($data['id']) . "'");
            session_regenerate_id();
            $_SESSION["logged"] = true;
            $_SESSION['id'] = $data['id'];
            $_SESSION['rank'] = $data['rank'];
            Registry::get('log')->write(8, 'com_user', 'NOW()', 'Creating session for user ' . $username,
                $data['id']);
            return true;
        } else {
            Registry::get('log')->write(2, 'com_user', 'NOW()', 'Login attempt failed for ' . $username,
                0);
            return false;
        }
    }
    
    /**
     * Do logout request
     * 
     * @return bool
     */
    public function doLogout(){
    	return session_destroy();
    }

    /**
     * Get username from user id
     * 
     * @param mixed $id
     * @return string $uid
     */
    public function getUsername($id = false)
    {
        $id = ($id !== false) ? $id : intval($_SESSION['id']);
        $query = parent::$db->query("SELECT username FROM {prefix}users WHERE id = '" .
            intval($id) . "'");
        $data = $query->fetch_array();
        return $data[0];
    }

    /**
     * Get display name from user id
     * 
     * @param mixed $id
     * @return string $name
     */
    public function getDisplayName($id = false)
    {
        $id = ($id !== false) ? $id : intval($_SESSION['id']);
        
        if(array_key_exists($id, (array)self::$_cache_display_name)){
            return self::$_cache_display_name[$id];
        }
        
        $query = parent::$db->query("SELECT display_name FROM {prefix}users WHERE id = '" .
            intval($id) . "'");
        $data = $query->fetch_array();
            if(empty($data[0])){
            	return Lang::getFront('com_user', 'notfound');
            }
        
        self::$_cache_display_name[$id] = $data[0];
        return $data[0];
    }

    /**
     * Get user avatar
     * 
     * @param mixed $id
     * @return string $avatar
     */
    public function getAvatar($id = false)
    {
        $id = ($id !== false) ? $id : intval($_SESSION['id']);
        
        if(array_key_exists($id, (array)self::$_cache_avatar)){
            return self::$_cache_avatar[$id];
        }
        
        $query = parent::$db->query("SELECT avatar FROM {prefix}users WHERE id = '" .
            intval($id) . "'");
        $data = $query->fetch_array();
        
        self::$_cache_avatar[$id] = $data[0];
        return $data[0];
    }

    /**
     * Get all user data from user id
     * 
     * @param mixed $id
     * @return array $data
     */
    public function getUserData($id = false)
    {
        $id = ($id !== false) ? $id : intval($_SESSION['id']);
        $query = parent::$db->query("SELECT * FROM {prefix}users WHERE id = '" . intval
            ($id) . "'");
        $data = $query->fetch_array();
        return $data;
    }

    /**
     * Get user permission from user id
     * 
     * @param mixed $id
     * @param mixed $format
     * @return int $result[0], string $text
     */
    public function getUserPerms($id = false, $format = 'simple')
    {
        $id = ($id !== false) ? $id : intval($_SESSION['id']);
        $query = parent::$db->query("SELECT rank FROM {prefix}users WHERE id = '" .
            intval($id) . "'");
        $result = $query->fetch_array();
        if ($format == 'simple') {
            return $result[0];
        } elseif ($format == 'text') {
            return self::rank2text($result[0]);
        }

    }

    /**
     * Return a text version of rank
     * 
     * @param mixed $rank
     * @return string $text
     */
    public function rank2text($rank)
    {
        switch ($rank) {
            case 0:
                return Lang::getFront('com_user', 'anonymous');
                break;
            case 1:
                return Lang::getFront('com_user', 'logged');
                break;
            case 2:
                return Lang::getFront('com_user', 'moderator');
                break;
            case 3:
                return Lang::getFront('com_user', 'editor');
                break;
            case 4:
                return Lang::getFront('com_user', 'administrator');
                break;
        }
    }

    /**
     * Check is user logged
     * 
     * @return bool
     */
    public function isLogged()
    {
        if (intval($_SESSION['logged']) == 0) {
            return false;
        }
        return true;
    }


}

?>